import {Injectable} from '@angular/core';
import {ActivatedRouteSnapshot, Resolve, RouterStateSnapshot} from '@angular/router';
import {HttpService} from '../services/http.service';
import {UrlAndOPT} from '../common/url-and-opt.enum';
import {Observable} from 'rxjs/index';
import {AppConfig} from '../common/app-config.enum';
import {fromPromise} from 'rxjs/internal/observable/fromPromise';
import {Storage} from '@ionic/storage';
import {mergeMap} from 'rxjs/internal/operators';

@Injectable({
  providedIn: 'root'
})
export class MembershipResolve implements Resolve<any> {

  constructor(
    private storage: Storage,
    private service$: HttpService
  ) {
  }

  resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<any> {
    return fromPromise(this.storage.get(AppConfig.USER_INFO)) // 获取用户类型
      .pipe(
        mergeMap(userInfo => {
          let OPT = UrlAndOPT.auth_membership_get;
          if (userInfo[AppConfig.USER_TYPE_KEY] === 2) { // 判断是否为企业用户
            OPT = UrlAndOPT.auth_membership_enter_get;
          }
          return this.service$.doRequest(OPT);
        })
      );
  }

}
